<?
class Url{
	static function build_all($except=array(), $addition=false){
		if(is_array($except))
			$except+=array('form_block_id','ebug','abug','tbug','delcache','delscache','utm_source','utm_campaign','utm_content','utm_medium');
		else
			$except=array('form_block_id','ebug','abug','tbug','delcache','delscache','utm_source','utm_campaign','utm_content','utm_medium');
		
		$url  = '';
		$page = '';
		
		foreach($_POST as $key=>$value){
			if(!in_array($key, $except) && $key!='form_block_id'){
				if(!is_array($value)){
					if($key=='page'){
						$page=urlencode($value);
					}
					else/*if($value!='')*/
						$url.=($url?'&':'').urlencode($key).'='.urlencode($value);
				}
			}
		}
		
		foreach($_GET as $key=>$value){	
			
			if(!in_array($key, $except) && $key!='form_block_id' && !isset($_POST[$key])){
				
				if($key=='page')
				{
					$page=urlencode($value);
				}
				elseif(is_array($value))
					$url.=($url?'&':'').urlencode($key).'='.$value;
				else/*if($value!='')*/
					$url.=($url?'&':'').urlencode($key).'='.urlencode($value);
			}
		}
		
		if($addition){
			$url.=($url?'&':'').$addition;
		}
		
		if(!$page || $page=='home'){
			$url=($url?'?':'').$url;
		}
		else{
			$url='?page='.$page.($url?'&'.$url:'');
		}
		
		//return $url?$url:'./';WEB_DIR
		//return $url?$url:WEB_ROOT;
		//$url = AZRewrite::formatUrl($url);
		return $url?$url:WEB_DIR;
	}
	
	static function build_current($params=array(),$anchor=''){
		return Url::build((isset(AZNet::$page['name'])?AZNet::$page['name']:'home'),$params,$anchor);
	}
	
	static function build($page,$params=array(),$anchor=''){
		if($page=='home')
			$request_string = '';
		else{
			$request_string = '?page='.$page;
		}

		if ($params){
			foreach ($params as $param=>$value){
				if(is_numeric($param)){
					if(isset($_REQUEST[$value]) && $value!='page'){
						$request_string .= ($request_string?'&':'?').$value.'='.urlencode($_REQUEST[$value]);
					}
				}
				elseif($param!='page'){
					$request_string .= ($request_string?'&':'?').$param.'='.urlencode($value);
				}
			}
		}
		
		$request_string = AZRewrite::formatUrl($request_string);
		return WEB_DIR.$request_string.$anchor;
	}
	
	static function redirect_current($params=array(),$anchor = ''){
		Url::redirect(AZNet::$page['name'],$params,$anchor);
	}
	
	static function redirect_href($params=false){
		if(Url::check('href')){
			Url::redirect_url(Url::attach($_REQUEST['href'],$params));
			return true;
		}
	}
	
	static function check($params){
		if(!is_array($params)){
			$params=array(0=>$params);
		}
		
		foreach($params as $param=>$value){
			if(is_numeric($param)){
				if(!isset($_REQUEST[$value])){
					return false;
				}
			}
			else{
				if(!isset($_REQUEST[$param])){
					return false;
				}
				else{
					if($_REQUEST[$param]!=$value){
						return false;
					}
				}
			}
		}
		return true;
	}
	
	//Chuyen sang trang chi ra voi $url
	static function redirect($page=false,$params=false, $anchor=''){
		if(!$page && !$params){
			Url::redirect_url();
		}
		else{
			Url::redirect_url(Url::build($page, $params,$anchor));
		}
	}
	
	static function redirect_url($url=false,$type = false){
		/*if(!$url || $url==''){
			$url='?'.$_SERVER['QUERY_STRING'];
		}*/
		
		if($url != ''){
			if(strpos($url,WEB_ROOT) === 0){
				$url = substr($url,strlen(WEB_ROOT));
			}
			if(strpos($url,WEB_DIR) === 0){
				$url = substr($url,strlen(WEB_DIR));
			}
			/*if (REWRITE_ON){
				$url= AZRewrite::doReplace($url);
				unset($rewrite);
			}*/
			$url = AZRewrite::formatUrl($url);
		}
		else{
			$url = '';
		}	
		
		
		if($type == 301){
			Header( "HTTP/1.1 301 Moved Permanently" );
		}
		
		header('Location:'.WEB_DIR.$url);
		System::halt();
	}
	
	static function access_denied(){
		Url::redirect('home');
		//header("Location: ".WEB_ROOT."err/error.html");
		//die();
	}
	
	static function get($name,$default=''){
		if (isset($_REQUEST[$name])){
			return $_REQUEST[$name];
		}
		elseif (isset($_POST[$name])){
			return $_POST[$name];
		}
		elseif(isset($_GET[$name])){
			return $_GET[$name];
		}
		else{
			return $default;		
		}
	}
	
	static function sget($name,$default=''){
		return strtr(Url::get($name, $default),array('"'=>'\\"'));
	}
	
	static function cdouble( $doubleval ){
	//echo $doubleval;
		//if(!is_numeric($doubleval))
		{			
			
			$doubleval=strtr($doubleval,array('.'=>''));
			$doubleval=strtr($doubleval,array(','=>'.'));
			
		}
		
		return doubleval($doubleval);
	}
	
	static function open_popup($url=false,$width=false,$height=false,$top=false,$left=false,$event=false,$resizable=0,$toolbar=0,$status=0,$scrollbars=0,$address_bar=0,$menubar=0){
		if(!$url)
			$url	='about:blank';
		elseif(strpos($url,'?')===false)
			$url.='?is_popup=1';
		else 
			$url.='&is_popup=1';
			
		if(!$width)
			$width	=300;
		if(!$height)
			$height	=400;
			
		if(!$top)
			$top	='\' + (screen.height -'.$height.')/2 +\'';		
		if(!$left)
			$left	='\' + (screen.width -'.$width.')/2 +\'';
		
		if(!$event)
			$event	='onclick';	

		return $event.'="window.open(\''.$url.'\' ,\'\',\'status='.$status.',toolbar='.$toolbar.',scrollbars='.$scrollbars.',resizable='.$resizable.',width='.$width.',height='.$height.',top='.$top.',left='.$left.',location ='.$address_bar.',menubar ='.$menubar.'\');"';	
	}
	
	
	static function redirect_js($close_on_finished=false,$msg='',$page=false,$params=false,$return=false)
	{
		
		$redirect_java='<script>';
		if($msg!='')
			$redirect_java.='alert("'.$msg.'");';
			
		if($close_on_finished)
			$redirect_java.='window.close();';
		else 
			$redirect_java.='window.location=\''.self::build($page,$params).'\';';
		
		$redirect_java.='</script>';
		
		if($return)
			return 	$redirect_java;
		else 
		{
			echo 	$redirect_java; 
			exit;
		}
	}
	
	static function currency($value, $currency_id = 1)
	{
		if(!$value)
		return "Liên hệ";
		
		$value	=	number_format($value,0,',','.');//$value	= 	str_replace(',00', '', $value);
		
		if($currency_id==1)
		{
			return $value.' VNĐ';
		}
		else{
			return $value.' USD';
		}
	}
	
	static function currency_format($value)
	{
		if(!$value)
		return "";
		
		return	number_format($value,0,',','.');//$value	= str_replace(',00', '', $value);return 	$value;
	}
	
}
?>